package io.sxl.modules.generator.service.impl;

import io.sxl.common.utils.R;
import io.sxl.modules.generator.entityx.HouseShowEntity;
import io.sxl.modules.generator.entityx.IsFinish;
import io.sxl.modules.generator.entityx.XiaoquAndNum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.sxl.common.utils.PageUtils;
import io.sxl.common.utils.Query;

import io.sxl.modules.generator.dao.HouseDao;
import io.sxl.modules.generator.entity.HouseEntity;
import io.sxl.modules.generator.service.HouseService;

@Slf4j
@Service
public class HouseServiceImpl extends ServiceImpl<HouseDao, HouseEntity> implements HouseService {
    @Autowired
    private HouseDao dao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HouseEntity> page = this.page(
                new Query<HouseEntity>().getPage(params),
                new QueryWrapper<HouseEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 根据输入的 户主姓名，所属小区，所属项目(参数为为空字符串不作为判断条件) 来动态查询房屋的展示信息（HouseShowEntity）
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryByHousePage(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        params.replace("page", (page - 1) * limit);
        List<HouseShowEntity> houseShowEntities = dao.queryByHousePage(params);
        PageUtils pageUtils = new PageUtils(houseShowEntities, houseShowEntities.size(), limit, page);

        return pageUtils;
    }

    @Override
    public Integer queryByHousePageCount(Map<String, Object> params) {
        return dao.queryByHousePageCount(params);
    }

    @Override
    public PageUtils queryByProIdAndHostNotNull(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        params.replace("page", (page - 1) * limit);
        List<HouseShowEntity> houseShowEntities = dao.queryByProIdAndHostNotNull(params);
        PageUtils pageUtils = new PageUtils(houseShowEntities, houseShowEntities.size(), limit, page);
        return pageUtils;
    }

    @Override
    public Integer queryByProIdAndHostNotNullCount(Map<String, Object> params) {
        return dao.queryByProIdAndHostNotNullCount(params);
    }


    @Override
    public PageUtils queryLikeNumber(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        params.replace("page", (page - 1) * limit);
        List<HouseShowEntity> houseShowEntities = dao.queryLikeNumber(params);
        PageUtils pageUtils = new PageUtils(houseShowEntities, houseShowEntities.size(), limit, page);
        Integer count = dao.queryLikeNumberCount(params);
        pageUtils.setTotalCount(count);
        return pageUtils;
    }


    @Override
    public PageUtils queryLikeStr(Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        params.replace("page", (page - 1) * limit);
        List<HouseShowEntity> houseShowEntities = dao.queryLikeStr(params);
        PageUtils pageUtils = new PageUtils(houseShowEntities, houseShowEntities.size(), limit, page);
        Integer count = dao.queryLikeStrCount(params);
        pageUtils.setTotalCount(count);
        return pageUtils;
    }

    @Override
    public PageUtils queryHasFenpei(Map<String, Object> params) {

        IPage<HouseEntity> page = this.page(
                new Query<HouseEntity>().getPage(params),
                new QueryWrapper<HouseEntity>().isNull("project_id")
        );
        return new PageUtils(page);
    }


    @Override
    public PageUtils queryShowHouse(Map<String, Object> params) {

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        params.replace("page", (page - 1) * limit);
        List<HouseShowEntity> houseShow = dao.getHouseShow(params);
        return new PageUtils(houseShow, houseShow.size(), limit, page);

    }

    @Override
    public Map<String, Integer> queryFenpeidiaobozonglan() {


        int finish = count(new QueryWrapper<HouseEntity>().isNotNull("host_id").isNotNull("project_id"));
        int half = count(new QueryWrapper<HouseEntity>().isNotNull("project_id").isNull("host_id"));
        int not = count(new QueryWrapper<HouseEntity>().isNull("project_id").isNull("host_id"));

        Map<String, Integer> map = new HashMap<>();
        map.put("finish", finish);
        map.put("half", half);
        map.put("not", not);

        return map;
    }





    /*自定义保存方法（加入当前时间）*/
    @Override
    public boolean save(HouseEntity entity) {
        if (entity.getCreateTime() == null) {
            log.info("creteTime is null");
            Date date = new Date();

            entity.setCreateTime(date);


            return super.save(entity);
        }
        return super.save(entity);
    }


    /**
     * 这是关于Echarts柱状统计图的查询相关的操作
     * 这是 从数据库的查询操作有冗余数据（根据小区查询到的小区总房间数没用用到），
     *
     * 但是暂时不进行删除
     *可能以后会使用到
     * （如果进行删除那么entityx包下的实体类也需要进行删除）
     * @return
     */
    @Override
    public R mainXiaoqu(){
        List<String> xiaoqu = new ArrayList<>();

        List<Integer> data1 = new ArrayList<>();

        List<Integer> data2 = new ArrayList<>();

        List<Integer> data3 = new ArrayList<>();




        List<XiaoquAndNum> list = dao.queryMainXiaoqu();
        IsFinish isFinish[] = new IsFinish[5];
        for (int i = 0; i < list.size(); i++) {
            XiaoquAndNum xiaoquAndNum = list.get(i);
            String name = xiaoquAndNum.getXiaoqu();
            xiaoqu.add(name);
            isFinish[i] = dao.queryIsFinish(name);
        }
        for (int i = 0; i < isFinish.length; i++) {
            data1.add(isFinish[i].getFinish());
            data2.add(isFinish[i].getHalf());
            data3.add(isFinish[i].getNot());
        }



        return  new R().put("xiaoqu",xiaoqu).put("data1",data1).put("data2",data2).put("data3",data3);
    }



}
